/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import gestionnaire.FilmPanier;
import gestionnaire.GestionnairePanier;
import gestionnaire.Gestionnaires;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import modeles.Commande;
import modeles.Film;

/**
 *
 * @author MAZID
 */
@WebServlet(name = "ServletPanier", urlPatterns = {"/ServletPanier"})
public class ServletPanier extends HttpServlet {
    @EJB
    private GestionnairePanier gestionnairePanier;
    private Gestionnaires gestionnaires;
    
    

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
            String page =request.getHeader("Referer");
            HttpSession session =request.getSession(true);
            String action=request.getParameter("action");
            String forwardTo = "";
            FilmPanier panier = null;
            
            if(action==null || action.equalsIgnoreCase(""))
            {
                action="lister";
            }
            if(action!=null){

            if(action.equals("lister"))
            {
                Collection<FilmPanier> contenuPanier =gestionnairePanier.getContenuPanier();
                double prixTotalPanier = gestionnairePanier.calculPrixTotal();
                int nbProduitPanier = gestionnairePanier.getNbProduit();
                double prixTotalTTC = gestionnairePanier.calculPrixTotalTVA();

                if(contenuPanier!=null){
                    request.setAttribute("listerContenu", contenuPanier);
                    request.setAttribute("prixTotal", prixTotalPanier);
                    request.setAttribute("nbProduitPanier", nbProduitPanier);
                    request.setAttribute("prixTotalTTC", prixTotalTTC);
                    //forwardTo="ServletFilm?action=afficherPanier";
                    RequestDispatcher dp = request.getRequestDispatcher("/ServletFilm?action=afficherPanier");
                    dp.forward(request, response);


                }
                else{
                    RequestDispatcher dp = request.getRequestDispatcher("/ServletFilm?action=panierVide");
                    dp.forward(request, response);
                    //forwardTo="ServletFilm?action=panierVide";
                }
            }

            if(action.equals("ajouter"))
            {   
                int id = Integer.parseInt(request.getParameter("idFilm"));
                
                // On récupert le panier en session
                panier = (FilmPanier)request.getSession().getAttribute("panier");
                // Si le panier est null on le crée
                    if(panier==null)
                    {
                            panier=new FilmPanier();
                            
                            
                    }else{
                        gestionnairePanier.ajouterFilm(id);
                        int nbFilm = gestionnairePanier.getNbProduit();
                        request.getSession().setAttribute("panier", panier);
                        request.getSession().setAttribute("nbFilms",nbFilm);
                    }
                                    
                
                
                // System.out.println("le nombre de film est: "+nbFilm);
                //session.setAttribute("nbFilms", nbFilm);
                response.sendRedirect(page);
            }

            if(action.equals("supprimer"))
            {
                int id = Integer.parseInt(request.getParameter("id"));
                gestionnairePanier.supprimerFilmPanier(id);
                response.sendRedirect(page);
            }
            if(action.equals("incrementer")){
                int id = Integer.parseInt(request.getParameter("id"));
                gestionnairePanier.incrementQteFilm(id);
                response.sendRedirect(page);
            }
            if(action.equals("decrementer")){
                int id = Integer.parseInt(request.getParameter("id"));
                gestionnairePanier.decrementeQteFilm(id);
                response.sendRedirect(page);
            }


        }
//            RequestDispatcher dp = request.getRequestDispatcher(forwardTo);
//            dp.forward(request, response);
                

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
